from odoo import fields, models, api


class SelectOrderPlanWizard(models.TransientModel):
    _name = 's.select.order.plan.wizard'
    _description = 'Select sales order'

    line_ids = fields.One2many('s.select.order.plan.wizard.line', 'order_id')

    @api.model
    def default_get(self, fields_list):
        result = super().default_get(fields_list)
        line_value_list = []
        sale_line_ids = self.env['sale.order.line'].search([
            ('product_id.detailed_type', '=', 'product'),
            ('order_id.state', '=', 'sale'),
            ('s_remaining_planned_qty', '>', 0),
        ])
        for line in sale_line_ids:
            line_value_list.append((0, 0, {
                'sale_id': line.order_id.id,
                'sale_order_line': line.id,
                'partner_id': line.order_id.partner_id.id,
                'product_id': line.product_id.id,
                'order_qty': line.product_uom_qty,
                'planned_qty': line.s_planned_qty,
                'remaining_planned_qty': line.s_remaining_planned_qty,
                'order_type': 'sale',
                'planned_receiving_date': line.order_id.planned_receiving_date
            }))
        result['line_ids'] = line_value_list
        return result


class SelectOrderPlanWizardLine(models.TransientModel):
    _name = 's.select.order.plan.wizard.line'
    _description = 'Select sales order line'

    order_id = fields.Many2one('s.select.order.plan.wizard')
    sale_id = fields.Many2one('sale.order')
    sale_order_line = fields.Many2one('sale.order.line')
    partner_id = fields.Many2one('res.partner', 'Customer', related='sale_id.partner_id')
    product_id = fields.Many2one('product.product', string='Product')
    order_qty = fields.Float(string='Order Qty')
    planned_qty = fields.Float(string='Planned quantity')
    remaining_planned_qty = fields.Float(string='Remaining planned quantity')
    order_type = fields.Selection([('sale', 'Sale'), ('dev', 'R&D')])
    planned_receiving_date = fields.Datetime(string='Planned Receiving Date')

    @api.model
    def default_get(self, fields_list):
        return super().default_get(fields_list)

    def action_select_sale_to_wizard(self):
        ...